{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import asyncio\n",
    "\n",
    "class AsyncIteratorWrapper:\n",
    "    def __init__(self, obj):\n",
    "        self._it = iter(obj)\n",
    " \n",
    "    def __aiter__(self):\n",
    "        return self\n",
    " \n",
    "    async def __anext__(self):\n",
    "        try:\n",
    "            value = next(self._it)\n",
    "        except StopIteration:\n",
    "            raise StopAsyncIteration\n",
    "        return value"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# test"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Test AsyncIteratorWrapper "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "123\n",
      "WWWWWW\n",
      "(0, None)\n",
      "如果这是异步的哪么就应该在上面之前打印出来\n",
      "WWWWWW\n",
      "(1, None)\n",
      "WWWWWW\n",
      "(2, None)\n",
      "如果这是异步的哪么就应该在上面之前打印出来\n",
      "(3, None)\n"
     ]
    }
   ],
   "source": [
    "import asyncio\n",
    "import time\n",
    "def showWdelay(iterator):\n",
    "    time.sleep(1)\n",
    "    print(\"WWWWWW\")\n",
    "    #return AsyncIteratorWrapper(iterator)\n",
    "    return AsyncIteratorWrapper(iterator).__aiter__()\n",
    "#     async for i in AsyncIteratorWrapper(iterator):\n",
    "#         print(i)\n",
    "#         print(\"A\")\n",
    "##         return i\n",
    "        \n",
    "async def test():\n",
    "    a=( (i,time.sleep(1)) for i in range(10))\n",
    " \n",
    "    print( await showWdelay(a).__anext__())\n",
    "    print( \"如果这是异步的哪么就应该在上面之前打印出来\")\n",
    "    print( await showWdelay(a).__anext__())\n",
    "    aa=showWdelay(a)\n",
    "    print( await aa.__anext__())\n",
    "    print( \"如果这是异步的哪么就应该在上面之前打印出来\")\n",
    "    print( await aa.__anext__())\n",
    "#     print(await showWdelay(a))\n",
    "#     print(\"插入一条\")\n",
    "#     print(await showWdelay(a))\n",
    "#     print(await showWdelay(a))\n",
    "\n",
    "        \n",
    "if __name__ == '__main__':\n",
    "    loop = asyncio.get_event_loop() # 开始事件循环\n",
    "\n",
    "    \n",
    "    loop.create_task(test())\n",
    "    print(123)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 阻塞事件的影响"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "aaa\n"
     ]
    }
   ],
   "source": [
    "# await \n",
    "import asyncio\n",
    "import time\n",
    "async def putInCh():\n",
    "    \n",
    "    for i in range(10):\n",
    "        time.sleep(1)\n",
    "\n",
    "async def test():\n",
    "    await putInCh()\n",
    "    for i in range(10):\n",
    "        print( i)\n",
    "loop = asyncio.get_event_loop() # 开始事件循环\n",
    "\n",
    "    \n",
    "loop.create_task(test())\n",
    "print(\"aaa\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'1243'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"1243\".encode()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
